package com.wyc.serviceDriverUser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wyc.internalCommon.constant.CarConstant;
import com.wyc.internalCommon.constant.CommonStatusEnum;
import com.wyc.internalCommon.constant.DriverUserConstant;
import com.wyc.internalCommon.response.DriverOrderResponse;
import com.wyc.internalCommon.result.ApiResult;
import com.wyc.serviceDriverUser.dto.Car;
import com.wyc.serviceDriverUser.dto.DriverCarBindingRelationship;
import com.wyc.serviceDriverUser.dto.DriverUser;
import com.wyc.serviceDriverUser.dto.DriverUserWorkStatus;
import com.wyc.serviceDriverUser.mapper.CarMapper;
import com.wyc.serviceDriverUser.mapper.DriverCarBindingRelationshipMapper;
import com.wyc.serviceDriverUser.mapper.DriverUserMapper;
import com.wyc.serviceDriverUser.mapper.DriverUserWorkStatusMapper;
import com.wyc.serviceDriverUser.service.DriverCarBindingRelationshipService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * <p>
 * 司机绑定车辆信息表 服务实现类
 * </p>
 *
 * @author li
 * @since 2023-03-26
 */
@Service
public class DriverCarBindingRelationshipServiceImpl extends ServiceImpl<DriverCarBindingRelationshipMapper, DriverCarBindingRelationship> implements DriverCarBindingRelationshipService {

    @Autowired
    private DriverCarBindingRelationshipMapper driverCarBindingRelationshipMapper;

    @Autowired
    private DriverUserWorkStatusMapper driverUserWorkStatusMapper;

    @Autowired
    private DriverUserMapper driverUserMapper;

    @Autowired
    private CarMapper carMapper;

    /**
     * 司机绑定车辆
     * @param driverCarBindingRelationship
     * @return
     */
    @Override
    public ApiResult DriverBindCar(DriverCarBindingRelationship driverCarBindingRelationship) {
        // 司机id
        Long driverId = driverCarBindingRelationship.getDriverId();
        // 车俩id
        Long carId = driverCarBindingRelationship.getCarId();
        QueryWrapper<DriverCarBindingRelationship> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("driver_id",driverId)
                .eq("bind_state", CarConstant.DriverBindCarStatus.DRIVER_CAR_BIND);
        // 一个司机只能绑定一辆状态是绑定状态的车辆
        Integer count = driverCarBindingRelationshipMapper.selectCount(queryWrapper);
        if (count > 0){
            // 司机已经绑定了其他车辆
            return ApiResult.error(CommonStatusEnum.DRIVER_CAR_BIND_ERROR.getCode(),CommonStatusEnum.DRIVER_CAR_BIND_ERROR.getMsg());
        }

        // 根据司机id和车辆id查询是否有绑定信息  无，新增，有，修改
        QueryWrapper<DriverCarBindingRelationship> driverCarQueryWrapper = new QueryWrapper<>();
        driverCarQueryWrapper.eq("driver_id",driverId)
                .eq("car_id",carId);
        Integer existsCount = driverCarBindingRelationshipMapper.selectCount(driverCarQueryWrapper);
        // 设置绑定状态
        driverCarBindingRelationship.setBindState(CarConstant.DriverBindCarStatus.DRIVER_CAR_BIND);
        driverCarBindingRelationship.setBindingTime(LocalDateTime.now());
        if (existsCount > 0){
            // 修改绑定状态
            UpdateWrapper<DriverCarBindingRelationship> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("driver_id",driverId)
                            .eq("car_id",carId);
            driverCarBindingRelationshipMapper.update(driverCarBindingRelationship,updateWrapper);
        }else {
            // 新增司机车辆绑定
            driverCarBindingRelationshipMapper.insert(driverCarBindingRelationship);
        }

        return ApiResult.success();
    }

    /**
     * 司机解绑车辆
     * @param driverCarBindingRelationship
     * @return
     */
    @Override
    public ApiResult DriverUnbindCar(DriverCarBindingRelationship driverCarBindingRelationship) {
        // 司机id
        Long driverId = driverCarBindingRelationship.getDriverId();
        // 车俩id
        Long carId = driverCarBindingRelationship.getCarId();
        // 根据司机id和车辆id查询是否有绑定信息  null 没有绑定车辆， 有 则 修改绑定状态
        QueryWrapper<DriverCarBindingRelationship> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("driver_id",driverId)
                .eq("car_id", carId);
        // 一个司机只能绑定一辆状态是绑定状态的车辆
        Integer count = driverCarBindingRelationshipMapper.selectCount(queryWrapper);
        if (count > 0){
            // 设置解绑状态
            driverCarBindingRelationship.setBindState(CarConstant.DriverBindCarStatus.DRIVER_CAR_UNBIND);
            driverCarBindingRelationship.setUnBindingTime(LocalDateTime.now());
            // 存在绑定信息--修改绑定状态
            UpdateWrapper<DriverCarBindingRelationship> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("driver_id",driverId)
                    .eq("car_id",carId);
            driverCarBindingRelationshipMapper.update(driverCarBindingRelationship,updateWrapper);
        }else {
            return ApiResult.error(CommonStatusEnum.DRIVER_CAR_BIND_NOT_EXISTS.getCode(),CommonStatusEnum.DRIVER_CAR_BIND_NOT_EXISTS.getMsg());
        }
        return ApiResult.success();
    }

    /**
     * 根据车辆id查询可接单的司机信息
     * @param carId
     * @return
     */
    @Override
    public ApiResult<DriverOrderResponse> getAvailableDriverByCarId(Long carId) {
        // 根据车辆id查询出绑定状态的司机id
        QueryWrapper<DriverCarBindingRelationship> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("car_id",carId).eq("bind_state",CarConstant.DriverBindCarStatus.DRIVER_CAR_BIND);
        DriverCarBindingRelationship one = driverCarBindingRelationshipMapper.selectOne(queryWrapper);
        if (null == one){
            return ApiResult.error(CommonStatusEnum.CAR_NOT_BIND_DRICER.getCode(),CommonStatusEnum.CAR_NOT_BIND_DRICER.getMsg());
        }
        // 司机id
        Long driverId = one.getDriverId();

        // 然后查询司机工作状态是出车状态的司机
        QueryWrapper<DriverUserWorkStatus> workStatusQueryWrapper = new QueryWrapper<>();
        workStatusQueryWrapper.eq("driver_id",driverId)
                        .eq("work_status", DriverUserConstant.WorkStatus.WORK_STATUS_START);
        DriverUserWorkStatus driverUserWorkStatus = driverUserWorkStatusMapper.selectOne(workStatusQueryWrapper);

        if (null == driverUserWorkStatus){
            // 没有可用的司机
            return ApiResult.error(CommonStatusEnum.AVAILABLE_DRIVER_EMPTY.getCode(),CommonStatusEnum.AVAILABLE_DRIVER_EMPTY.getMsg());
        }
        QueryWrapper<DriverUser> driverQueryWrapper = new QueryWrapper<>();
        driverQueryWrapper.eq("id",driverId);
        // 查询司机信息
        DriverUser driverUser = driverUserMapper.selectOne(driverQueryWrapper);

        DriverOrderResponse driverOrderResponse = new DriverOrderResponse();
        driverOrderResponse.setCarId(carId);
        driverOrderResponse.setDriverId(driverId);
        driverOrderResponse.setDriverPhone(driverUser.getDriverPhone());
        driverOrderResponse.setLicenseId(driverUser.getLicenseId());

        return ApiResult.success(driverOrderResponse);
    }
}
